package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/**
 * 翻牌 - 抽奖 下一轮
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardNext, classtype = ActiveMessage.ActFlopCardNextRequest.class)
public class CMD_Active_翻牌_抽奖_Next extends Command<ActiveMessage.ActFlopCardNextRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_抽奖_Next.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardNextRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActFlopCardNextResponse.Builder response = ActiveMessage.ActFlopCardNextResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getSelectid() == 0) {
			send(messageSource, response, Result.ActNOFinish);
			return;
		}
		
		for(FlopCard fc : am.getFcmap().values()) {
			if(fc.isGrand() && fc.isFlop()) {
				am.fcnext();
				send(messageSource, response, Result.Success);
				return;
			}
		}
		send(messageSource, response, Result.ActNOFinish);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardNextResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
